package Tetris;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
//import java.io.IOException;
import java.awt.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class TetrisMain extends Canvas implements Runnable, KeyListener{

	public static final int WIDTH=400, HEIGHT=565;	
	//private Image[] tetrisBlocks;
	public static void main(String[] args) {
		final JFrame fr = new JFrame("Tetris");
		fr.setSize(WIDTH,HEIGHT);
		fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		fr.setLocationRelativeTo(null);
		fr.setResizable(false);
		fr.setLayout(null);
		
		Keygether.loadkey();
		try {
			Config.loadConfig();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		JMenuBar bar = new JMenuBar();
		bar.setBounds(0, 0, WIDTH, 25);
		
		JMenu file = new JMenu("File");
		file.setBounds(0, 0, 45, 24);
		
		JMenuItem newGame= new JMenuItem("New Game");
		newGame.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// code new game
				System.out.println("Starting New Game...");
			}
		});
		
		JMenuItem highScore= new JMenuItem("Highscore");
		highScore.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// code high score
				int highScore=0;
				final JFrame alert = new JFrame("High Score");
				alert.setSize(200,150);
				alert.setLayout(null);
				alert.setLocationRelativeTo(null);
				
				JLabel score= new JLabel("The High Score is: "+highScore);
				score.setBounds(0, 0, 200, 50);
				
				JButton okButton= new JButton("OK");
				okButton.setBounds(50, 80, 100, 30);
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						alert.dispose();
					}
				});
				alert.add(score);
				alert.add(okButton);
				alert.setResizable(false);
				alert.setVisible(true);
			}
		});
		
		JMenuItem exit= new JMenuItem("Exit");
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// code exit
				System.out.println("Closing...");
				System.exit(0);
			}
		});
		
		JMenuItem option = new JMenuItem("Option");
		option.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Config.openconfig(fr);
				
			}
		});
		
		
		TetrisMain tm = new TetrisMain();
		tm.setBounds(0, 25, WIDTH, HEIGHT-25);
		
		fr.add(tm);
		file.add(newGame);
		file.add(highScore);
		file.add(option);
		file.add(exit);
		bar.add(file);
		fr.add(bar);
		fr.setVisible(true);
		tm.start();
	}

	public void start(){
		Thread t = new Thread(this);
		t.setPriority(Thread.MAX_PRIORITY);
		t.start();
	}
	
	public void run() {
		//init();
		boolean running = true;
		while(running){
			update();
			BufferStrategy buf = getBufferStrategy();
			if(buf==null){
				createBufferStrategy(3);
				continue;
			}
			Graphics2D g = (Graphics2D) buf.getDrawGraphics();
			render(g);
			buf.show();
		}
		
	}
	
	public void init(){
		//requestFocus();
		/*try{
			tetrisBlocks=ImageLoader.loadImages("/tetris.png", 25);
		}
		catch(IOException e){
			System.out.println("Error loading in tetris.png");
			System.exit(1);
		}*/
	}
	
	public void update(){
		
	}

	public void render(Graphics2D g){
		/*g.setColor(Color.BLACK);
		g.fillRect(0, 0, WIDTH, HEIGHT);	
		g.setFont(new Font("Calibri",Font.PLAIN,20));
		g.setColor(Color.WHITE);
		g.drawString("Tetris", 160, 50);*/
		//g.drawImage(tetrisBlocks[0], 100, 100, 25, 25, null);
		Toolkit t = Toolkit.getDefaultToolkit();
		Image i = t.getImage("res.maudo.png");
        g.drawImage(i, 200, 200, this); 
		
	}
	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
